import Vue from 'vue'
import Router from 'vue-router'
import Welcome from './components/welcome.vue'
import Login from './views/login.vue'
import Home from './views/home.vue'
import User from './components/user/user.vue'
import Goods from './components/goods/goodList.vue'
import Rights from './components/power/rights.vue'
import Roles from './components/power/roles.vue'
import GoodsCategory from './components/goods/goodsCategory.vue'
import goodsAdd from './components/goods/addGoods.vue'
import Params from './components/goods/params.vue'
import Orders from './components/orders/orders.vue'
import Reports from './components/report/reports.vue'

Vue.use(Router)

const router = new Router({
  // mode: 'history',
  routes: [
    {
      path: '/',
      redirect: '/home'
    },
    {
      path: '/login',
      name: 'login',
      component: Login
    },
    {
      path: '/home',
      component: Home,
      redirect: '/welcome',
      children: [
        {
          path: '/welcome',
          component: Welcome
        },
        {
          path: '/users',
          component: User
        },
        {
          path: '/goods',
          component: Goods
        },
        {
          path: '/categories',
          component: GoodsCategory
        },
        {
          path: '/rights',
          component: Rights
        },
        {
          path: '/roles',
          component: Roles
        },
        {
          path: '/goods/add',
          component: goodsAdd
        },
        {
          path: '/params',
          component: Params
        },
        {
          path: '/orders',
          component: Orders
        },
        {
          path: '/reports',
          component: Reports
        }
      ]
    }
  ]
})
// 配置导航守卫
router.beforeEach((to, from, next) => {
  if (to.path === '/login') return next()
  let str = window.sessionStorage.getItem('token')
  if (!str) return next('/login')
  next()
})
export default router
